articles

Home / DeveloperSection / Articles / Type Conversion (Type casting) in C#

Type Conversion (Type casting) in C#

Vijay Shukla4925 08-May-2013

In this article I am trying to elaborate the concept of type casting in C#.

Type conversion is basically type casting, or converting one type of data to another type. In C#, type casting has two forms:

Implicit type conversion - these conversions are performed by C# in a type-safe manner. Examples are conversions from smaller to larger integral types, and conversions from derived classes to base classes.

Explicit type conversion - these conversions are done explicitly by users using the pre-defined functions. Explicit conversions require a cast operator.

C# Type Conversion Methods

C# provides the following built-in type conversion methods:

Methods
Description

ToBoolean

Converts a type to a Boolean value, where possible.

ToByte

Converts a type to a byte.

ToChar

Converts a type to a single Unicode character, where possible.

ToDateTime

Converts a type (integer or string type) to date-time structures.

ToDecimal

Converts a floating point or integer type to a decimal type.

ToDouble

Converts a type to a double type.

ToInt16

Converts a type to a 16-bit integer.

ToInt32

Converts a type to a 32-bit integer.

ToInt64

Converts a type to a 64-bit integer.

ToSbyte

Converts a type to a signed byte type.

ToSingle

Converts a type to a small floating point number.

ToString

Converts a type to a string.

ToType

Converts a type to a specified type.

ToUInt16

Converts a type to an unsigned int type.

ToUInt32

Converts a type to an unsigned long type.

ToUInt64

Converts a type to an unsigned big integer.

 


Updated 30-Nov-2017

Leave Comment

Comments

Liked By